package com.wb.server.udp.server.handler;

import com.wb.server.processor.DataProcessorFactory;
import com.wb.tcp.common.utils.HexConvert;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author chenyong
 * @Description TODO
 * @Date 2022/12/14 11:19
 * @Version 1.0
 */
@Slf4j
public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
    private DataProcessorFactory processor;

    public UdpServerHandler(DataProcessorFactory processor) {
        this.processor=processor;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket) throws Exception {
        ByteBuf content = datagramPacket.content();
        int len = content.readableBytes();
        byte[] bytes = new byte[len];
        content.readBytes(bytes);
        String hexString = HexConvert.BinaryToHexString(bytes);
        log.info("receive message: {}",hexString);
        processor.processor(hexString);
    }
}
